package Tests;

import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.openqa.selenium.By;

import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import static java.lang.Thread.sleep;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class ForumListTest extends AutoTestUtils{
    @BeforeAll
    public static void setUpBeforeClass() throws Exception {
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

        //打开博客登录页面
        webDriver.get("http://127.0.0.1:9580/sign-in.html");
        // 在每次登录之后都要进行清空，然后才能重新输入
        webDriver.findElement(By.cssSelector("#username")).clear();
        webDriver.findElement(By.cssSelector("#password")).clear();

        //输入长高admin
        webDriver.findElement(By.cssSelector("#username")).sendKeys("张三");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //输入密码123
        webDriver.findElement(By.cssSelector("#password")).sendKeys("123");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //提交登录
        webDriver.findElement(By.cssSelector("#submit")).click();

        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
    }

    public static Stream<Arguments> Generator() {
        return Stream.of(Arguments.arguments("http://127.0.0.1:9580/index.html", "比特论坛", "张三写的"));
    }

    /*
     *论坛首页页帖子数不为0
     */
    @Order(1)
    @Test
    void ForumList(){

        //获取页面上所有帖子标题对应的元素
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        int title_num = webDriver.findElements(By.cssSelector("#artical-items-body")).size();
        //如果数量不为0，测试通过
        Assertions.assertNotEquals(0,title_num);
    }

    /*
     *论坛首页查看全文
     * url
     * 标题
     * 页面title是“比特论坛”
     */
    @Order(2)
    @ParameterizedTest
    @MethodSource("Generator")
    void ForumInspectAll(String expected_url, String expected_title, String expected_post_title) throws InterruptedException {
        //找到第一篇帖子的标题，点击查看全文
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        sleep(3000);
        webDriver.findElement(By.xpath("//*[@id=\"artical-items-body\"]/div[1]/div/div[2]/div[1]/a")).click();
        //获取当前页面url
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        String cur_url = webDriver.getCurrentUrl();
        //获取当前页面title
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        String cur_title = webDriver.getTitle();
        System.out.println(cur_title);
        //获取帖子标题
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        sleep(3000);
        String cur_post_title = webDriver.findElement(By.cssSelector("#details_article_title")).getText();

        System.out.println("post_title: "+cur_post_title+", cur_url: "+cur_url+", cur_title: "+cur_title);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);


        Assertions.assertEquals(expected_title, cur_title);
        Assertions.assertEquals(expected_post_title, cur_post_title);

        if(cur_url.contains(expected_url)) {
            System.out.println("测试通过");
        } else {
            System.out.println(cur_url);
            System.out.println("测试不通过");
        }
    }

    /*
     *退出账号
     */
    @Order(3)
    @Test
    void LogoutSuccess() throws InterruptedException {
        //进入我的帖子
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.dropdown > a")).click();
        webDriver.findElement(By.cssSelector("#index_user_logout")).click();
        sleep(3000);

        //获取当前url
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://127.0.0.1:9580/sign-in.html",cur_url);
        sleep(300);

        //重新登录，为下一步做准备
        //输入长高张三
        webDriver.findElement(By.cssSelector("#username")).sendKeys("张三");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //输入密码123
        webDriver.findElement(By.cssSelector("#password")).sendKeys("123");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //提交登录
        webDriver.findElement(By.cssSelector("#submit")).click();
    }
}
